package com.yumeng.framework.persistence.mybatis.helper;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wxd
 * @date 2022/9/6 18:53
 */
public class MybatisPlusHelper {

    private final static Map<String, String> tableNameCache = new ConcurrentHashMap<>();

    /**
     * 获取表名
     * @param entityClass
     * @return
     */
    public static String getTableName(Class entityClass){
        String key = entityClass.getName();
        String val = tableNameCache.get(key);
        if (val == null){
            TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
            if (tableInfo != null){
                val = tableInfo.getTableName();
                tableNameCache.put(key, val);
            }
        }
        return val;
    }

}
